package com.changgou.controllerr;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/*****
 * @Author: 黑马训练营
 * @Description: com.changgou
 ****/
@RestController
@RequestMapping(value = "/upload")
@CrossOrigin
public class FileController {


    /****
     * 文件上传
     * 1.SpringMVC文件上传
     *   MultipartFile
     * 2.转换成FastDFSFile
     * 3.调用FastDFSClient.upload()
     */
    @PostMapping
    public String upload(MultipartFile file) throws Exception{
        //转换成FastDFSFile
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),//文件名字  123.jpg
                file.getBytes(),            //文件字节数组
                StringUtils.getFilenameExtension(file.getOriginalFilename()));//123.jpg  扩展名
        //调用FastDFSClient.upload()
        String[] uploads = FastDFSClient.upload(fastDFSFile);

        //group  文件存储的信息
        //http://192.168.211.132:8080/group1/M00/00/00/xxxx.jpg   Storage中的Nginx的8080端口
        //return "http://192.168.211.132:8080/"+uploads[0]+"/"+uploads[1];
        return "http://images-changgou-java.itheima.net/"+uploads[0]+"/"+uploads[1];
    }
}
